/**
 * 
 */
package de.cbf.cam.dsa.tavern.carte.model.entities;

import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import de.cbf.cam.dsa.tavern.carte.model.ModelCarte;
import de.cbf.cam.dsa.tavern.carte.model.entities.helper.MinMax;
import de.cbf.cam.dsa.tavern.carte.model.entities.interfaces.IEditableEntity;
import de.cbf.cam.dsa.tavern.carte.model.entities.interfaces.INamedEntity;

/**
 * @author Surfer
 *
 */
public class MealType implements Cloneable,Comparable<MealType>,INamedEntity,IEditableEntity{
	public static final int PCE_INDEX_name		= 2;
	public static final int PCE_INDEX_price		= 3;
	public static final int PCE_INDEX_config	= 4;
	
	private String name  = ModelCarte.DEFAULT_MEAL_TYPE_NAME;
	private Double price = 0D;
	
	protected HashMap<String,MinMax> configProbabilityMap = new HashMap<String, MinMax>();
	
	public PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
	@Override
	public PropertyChangeSupport getPropertyChangeSupport() {
		return propertyChangeSupport;
	}
	/**
	 * 
	 */
	public MealType() {
		
	}
	
	public static Collection<Object> transformIteratorMealType(
			Iterator<MealType> iterator) {
		Collection<Object> resultList = new ArrayList<Object>();		
		while (iterator.hasNext()) {
			MealType ingredient = (MealType) iterator.next();
			resultList.add(ingredient);
		}
		return resultList;	
	}

	
	@Override
	public String toString() {
		return name+"("+price+")";	
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public IEditableEntity clone() {
		MealType clonedMealType = new MealType();
		clonedMealType.name = name;
		clonedMealType.price = price;
		clonedMealType.configProbabilityMap = (HashMap<String, MinMax>) configProbabilityMap.clone();
		return clonedMealType;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void takeOverValuesFrom(IEditableEntity entity)
			throws IllegalArgumentException {
		if (entity instanceof MealType) {
			MealType mealType = (MealType) entity;
			this.name = mealType.name;
			this.price = mealType.price;			
			this.configProbabilityMap.putAll((HashMap<String, MinMax>)mealType.configProbabilityMap.clone());
		}
		
	}
	@Override
	public int compareTo(MealType other) {
		return name.compareTo(other.name);		 
	}
	
	public String getName() {
		return name;
	}
	public void setName(String value) {
		if(value == null || value.equals(this.name)) return;
		String oldValue = this.name;
		this.name = value;
		propertyChangeSupport.fireIndexedPropertyChange("name",PCE_INDEX_name, oldValue,value);
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double value) {
		if(value == null || value.equals(this.price)) return;
		Double oldValue = this.price;
		this.price = value;
		propertyChangeSupport.fireIndexedPropertyChange("price",PCE_INDEX_price, oldValue,value);
	}
	public HashMap<String, MinMax> getConfigProbabilityMap() {
		return configProbabilityMap;
	}
	public void setConfigProbabilityMap(HashMap<String, MinMax> value) {
		if(value == null || value.equals(this.configProbabilityMap)) return;
		HashMap<String, MinMax> oldValue = this.configProbabilityMap;
		this.configProbabilityMap = value;
		propertyChangeSupport.fireIndexedPropertyChange("configuration",PCE_INDEX_config, oldValue,value);		
	}	
	public void putModifyConfigEntry(String key,MinMax value){		
		this.configProbabilityMap.put(key, value);
		propertyChangeSupport.fireIndexedPropertyChange("configuration",PCE_INDEX_config, null,configProbabilityMap);
	}
	public void removeConfigEntry(String key){		
		this.configProbabilityMap.remove(key);
		propertyChangeSupport.fireIndexedPropertyChange("configuration",PCE_INDEX_config, null,configProbabilityMap);		
	}

	

	

	
	

	

	
}
